import { request } from '../utils/request'

// 通道管理API

/**
 * 获取所有通道
 * @returns {Promise}
 */
export const getAllChannels = () => {
  return request.get('/onvif/channels')
}

/**
 * 根据设备ID获取通道列表
 * @param {number} deviceId - 设备ID
 * @returns {Promise}
 */
export const getChannelsByDevice = deviceId => {
  return request.get(`/onvif/channels/device/${deviceId}`)
}

/**
 * 根据设备ID字符串获取通道列表
 * @param {string} deviceId - 设备ID字符串
 * @returns {Promise}
 */
export const getChannelsByDeviceIdString = deviceId => {
  return request.get(`/onvif/channels/device-id/${deviceId}`)
}

/**
 * 根据ID获取通道详情
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const getChannelById = id => {
  return request.get(`/onvif/channels/${id}`)
}

/**
 * 更新通道信息
 * @param {number} id - 通道ID
 * @param {Object} channel - 通道信息
 * @param {string} channel.name - 通道名称
 * @param {string} channel.description - 描述
 * @param {boolean} channel.enabled - 是否启用
 * @returns {Promise}
 */
export const updateChannel = (id, channel) => {
  return request.put(`/onvif/channels/${id}`, channel)
}

/**
 * 启用/禁用通道
 * @param {number} id - 通道ID
 * @param {Object} data - 状态数据
 * @param {boolean} data.enabled - 是否启用
 * @returns {Promise}
 */
export const toggleChannelEnabled = (id, data) => {
  return request.patch(`/onvif/channels/${id}/enabled`, data)
}

/**
 * 删除通道
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const deleteChannel = id => {
  return request.delete(`/onvif/channels/${id}`)
}

/**
 * 根据状态获取通道列表
 * @param {string} status - 通道状态 (ACTIVE/INACTIVE/ERROR)
 * @returns {Promise}
 */
export const getChannelsByStatus = status => {
  return request.get(`/onvif/channels/by-status/${status}`)
}

/**
 * 获取活跃的通道列表
 * @returns {Promise}
 */
export const getActiveChannels = () => {
  return request.get('/onvif/channels/active')
}

/**
 * 根据设备ID获取活跃通道数量
 * @param {number} deviceId - 设备ID
 * @returns {Promise}
 */
export const getActiveChannelCountByDevice = deviceId => {
  return request.get(`/onvif/channels/device/${deviceId}/active-count`)
}

/**
 * 根据设备ID获取通道数量
 * @param {number} deviceId - 设备ID
 * @returns {Promise}
 */
export const getChannelCountByDevice = deviceId => {
  return request.get(`/onvif/channels/device/${deviceId}/count`)
}

/**
 * 刷新设备的所有通道信息
 * @param {string} deviceId - 设备ID
 * @returns {Promise}
 */
export const refreshChannelsByDevice = deviceId => {
  return request.post(
    `/onvif/channels/device-id/${deviceId}/refresh`,
    {},
    {
      loadingText: '正在刷新通道信息...'
    }
  )
}

/**
 * 测试通道连接
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const testChannelConnection = id => {
  return request.post(
    `/onvif/channels/${id}/test`,
    {},
    {
      loadingText: '正在测试通道连接...'
    }
  )
}

/**
 * 获取通道配置
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const getChannelConfig = id => {
  return request.get(`/onvif/channels/${id}/config`)
}

/**
 * 更新通道配置
 * @param {number} id - 通道ID
 * @param {Object} config - 通道配置
 * @returns {Promise}
 */
export const updateChannelConfig = (id, config) => {
  return request.put(`/onvif/channels/${id}/config`, config, {
    loadingText: '正在更新通道配置...'
  })
}

/**
 * 获取通道预设位列表
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const getChannelPresets = id => {
  return request.get(`/onvif/channels/${id}/presets`)
}

/**
 * 设置通道预设位
 * @param {number} id - 通道ID
 * @param {Object} preset - 预设位信息
 * @param {string} preset.name - 预设位名称
 * @param {number} preset.token - 预设位令牌
 * @returns {Promise}
 */
export const setChannelPreset = (id, preset) => {
  return request.post(`/onvif/channels/${id}/presets`, preset, {
    loadingText: '正在设置预设位...'
  })
}

/**
 * 调用通道预设位
 * @param {number} id - 通道ID
 * @param {number} presetToken - 预设位令牌
 * @returns {Promise}
 */
export const gotoChannelPreset = (id, presetToken) => {
  return request.post(
    `/onvif/channels/${id}/presets/${presetToken}/goto`,
    {},
    {
      loadingText: '正在调用预设位...'
    }
  )
}

/**
 * 删除通道预设位
 * @param {number} id - 通道ID
 * @param {number} presetToken - 预设位令牌
 * @returns {Promise}
 */
export const removeChannelPreset = (id, presetToken) => {
  return request.delete(`/onvif/channels/${id}/presets/${presetToken}`, {
    loadingText: '正在删除预设位...'
  })
}

/**
 * PTZ控制
 * @param {number} id - 通道ID
 * @param {Object} command - PTZ命令
 * @param {string} command.action - 动作 (move/stop/zoom)
 * @param {string} command.direction - 方向 (up/down/left/right/upleft/upright/downleft/downright)
 * @param {number} command.speed - 速度 (0-1)
 * @param {number} command.zoom - 缩放 (-1到1)
 * @returns {Promise}
 */
export const ptzControl = (id, command) => {
  return request.post(`/onvif/channels/${id}/ptz`, command)
}

/**
 * 获取通道录像列表
 * @param {number} id - 通道ID
 * @param {Object} params - 查询参数
 * @param {string} params.startTime - 开始时间
 * @param {string} params.endTime - 结束时间
 * @param {number} params.page - 页码
 * @param {number} params.size - 页大小
 * @returns {Promise}
 */
export const getChannelRecordings = (id, params = {}) => {
  return request.get(`/onvif/channels/${id}/recordings`, params)
}

/**
 * 开始录像
 * @param {number} id - 通道ID
 * @param {Object} config - 录像配置
 * @returns {Promise}
 */
export const startRecording = (id, config = {}) => {
  return request.post(`/onvif/channels/${id}/recordings/start`, config, {
    loadingText: '正在开始录像...'
  })
}

/**
 * 停止录像
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const stopRecording = id => {
  return request.post(
    `/onvif/channels/${id}/recordings/stop`,
    {},
    {
      loadingText: '正在停止录像...'
    }
  )
}

/**
 * 下载录像
 * @param {number} id - 通道ID
 * @param {string} recordingId - 录像ID
 * @returns {Promise}
 */
export const downloadRecording = (id, recordingId) => {
  return request.download(
    `/onvif/channels/${id}/recordings/${recordingId}/download`,
    {},
    {
      loadingText: '正在下载录像...'
    }
  )
}

/**
 * 删除录像
 * @param {number} id - 通道ID
 * @param {string} recordingId - 录像ID
 * @returns {Promise}
 */
export const deleteRecording = (id, recordingId) => {
  return request.delete(`/onvif/channels/${id}/recordings/${recordingId}`, {
    loadingText: '正在删除录像...'
  })
}

/**
 * 获取通道事件列表
 * @param {number} id - 通道ID
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getChannelEvents = (id, params = {}) => {
  return request.get(`/onvif/channels/${id}/events`, params)
}

/**
 * 订阅通道事件
 * @param {number} id - 通道ID
 * @param {Object} subscription - 订阅配置
 * @returns {Promise}
 */
export const subscribeChannelEvents = (id, subscription) => {
  return request.post(`/onvif/channels/${id}/events/subscribe`, subscription, {
    loadingText: '正在订阅事件...'
  })
}

/**
 * 取消订阅通道事件
 * @param {number} id - 通道ID
 * @returns {Promise}
 */
export const unsubscribeChannelEvents = id => {
  return request.post(
    `/onvif/channels/${id}/events/unsubscribe`,
    {},
    {
      loadingText: '正在取消订阅...'
    }
  )
}

/**
 * 批量操作通道
 * @param {Object} data - 批量操作数据
 * @param {number[]} data.ids - 通道ID列表
 * @param {string} data.action - 操作类型 (enable/disable/delete/refresh)
 * @returns {Promise}
 */
export const batchOperateChannels = data => {
  return request.post('/onvif/channels/batch', data, {
    loadingText: '正在执行批量操作...'
  })
}
